﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Shared.Models.SshClient.CommandResult
{
    public class LinuxRunningProcess: ILinuxCommandResultData<LinuxRunningProcess>
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string User { get; set; }
        /// <summary>
        /// 进程 ID
        /// </summary>
        public int Pid { get; set; }
        /// <summary>
        /// CPU 使用率
        /// </summary>
        public double CpuUsage { get; set; }
        /// <summary>
        /// 内存使用率
        /// </summary>
        public double MemoryUsage { get; set; }
        /// <summary>
        /// 虚拟内存大小
        /// </summary>
        public long VsZ { get; set; }
        /// <summary>
        /// 驻留集大小
        /// </summary>
        public long Rss { get; set; }
        /// <summary>
        /// 终端
        /// </summary>
        public string Tty { get; set; }
        /// <summary>
        /// 进程状态
        /// </summary>
        public string Stat { get; set; }
        /// <summary>
        /// 启动时间
        /// </summary>
        public string Start { get; set; }
        /// <summary>
        /// 运行时间
        /// </summary>
        public string Time { get; set; }
        /// <summary>
        /// 命令
        /// </summary>
        public string Command { get; set; }

        public List<LinuxRunningProcess> ParseResultDataList(string outData)
        {
            var processes = new List<LinuxRunningProcess>();
            var lines = outData.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            string pattern = @"^(\S+)\s+" +       // User
                   @"(\d+)\s+" +         // PID
                   @"([\d.]+)\s+" +      // CPU
                   @"([\d.]+)\s+" +      // Memory
                   @"(\d+)\s+" +         // VSZ
                   @"(\d+)\s+" +         // RSS
                   @"(\S+)\s+" +         // TTY
                   @"(\S+)\s+" +         // STAT
                   @"(\S+)\s+" +         // START
                   @"(\S+)\s+" +         // TIME
                   @"(.+)";              // COMMAND
            var regex = new Regex(pattern);

            foreach (var line in lines)
            {
                var match = regex.Match(line);
                if (match.Success)
                {
                    var process = new LinuxRunningProcess
                    {
                        User = match.Groups[1].Value,
                        Pid = int.Parse(match.Groups[2].Value),
                        CpuUsage = double.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture),
                        MemoryUsage = double.Parse(match.Groups[4].Value, CultureInfo.InvariantCulture),
                        VsZ = long.Parse(match.Groups[5].Value),
                        Rss = long.Parse(match.Groups[6].Value),
                        Tty = match.Groups[7].Value,
                        Stat = match.Groups[8].Value,
                        Start = match.Groups[9].Value,
                        Time = match.Groups[10].Value,
                        Command = match.Groups[11].Value.Trim()
                    };
                    processes.Add(process);
                }
            }
            return processes;
        }
    }
}
